@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

:: 添加标题
title Cursor ID Reset Tool

:: 设置日期时间格式
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set datetime=%%I
set "TIMESTAMP=%datetime:~0,4%%datetime:~4,2%%datetime:~6,2%_%datetime:~8,2%%datetime:~10,2%%datetime:~12,2%"

:: 创建备份目录
set "BACKUP_DIR=C:\Users\Administrator\AppData\Roaming\Cursor\User\globalStorage\backups"
if not exist "%BACKUP_DIR%" (
    mkdir "%BACKUP_DIR%"
    echo 创建备份目录: %BACKUP_DIR%
)

:: 生成新的GUID并修改注册表
for /f "tokens=*" %%a in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "[guid]::NewGuid().ToString().ToLower()"') do set "NEW_MACHINE_GUID=%%a"

:: 备份并修改注册表
reg export "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography" "%BACKUP_DIR%\MachineGuid_%TIMESTAMP%.txt" /y
if %errorlevel% neq 0 (
    echo 错误: 注册表备份失败
    goto :error
)
echo 注册表备份成功: %BACKUP_DIR%\MachineGuid_%TIMESTAMP%.txt

:: 使用PowerShell修改注册表（需要管理员权限）
powershell -NoProfile -ExecutionPolicy Bypass -Command "$path = 'HKLM:\SOFTWARE\Microsoft\Cryptography'; Set-ItemProperty -Path $path -Name 'MachineGuid' -Value '%NEW_MACHINE_GUID%' -Type String -Force"
if %errorlevel% neq 0 (
    echo 错误: 注册表修改失败
    goto :error
)

:: 验证修改是否成功（不显示注册表内容）
for /f "tokens=3" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography" /v MachineGuid') do set "CURRENT_GUID=%%a"
if "%CURRENT_GUID%" == "%NEW_MACHINE_GUID%" (
    echo 注册表修改成功
) else (
    echo 错误: 注册表修改验证失败
    goto :error
)

:: 处理storage.json文件
set "STORAGE_FILE=C:\Users\Administrator\AppData\Roaming\Cursor\User\globalStorage\storage.json"

:: 备份storage.json
copy "%STORAGE_FILE%" "%BACKUP_DIR%\storage_%TIMESTAMP%.json" >nul
if %errorlevel% neq 0 (
    echo 错误: storage.json备份失败
    goto :error
)
echo storage.json备份成功: %BACKUP_DIR%\storage_%TIMESTAMP%.json

:: 生成新的随机ID
for /f "tokens=*" %%a in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "[System.BitConverter]::ToString([System.Security.Cryptography.HashAlgorithm]::Create('SHA256').ComputeHash([System.Text.Encoding]::UTF8.GetBytes([System.Guid]::NewGuid().ToString()))).Replace('-','').ToLower()"') do set "NEW_MACHINE_ID=%%a"
for /f "tokens=*" %%a in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "[System.BitConverter]::ToString([System.Security.Cryptography.HashAlgorithm]::Create('SHA256').ComputeHash([System.Text.Encoding]::UTF8.GetBytes([System.Guid]::NewGuid().ToString()))).Replace('-','').ToLower()"') do set "NEW_MAC_ID=%%a"
for /f "tokens=*" %%a in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "$([guid]::NewGuid().ToString().ToUpper())"') do set "NEW_SQM_ID={%%a}"
for /f "tokens=*" %%a in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "[guid]::NewGuid().ToString()"') do set "NEW_DEVICE_ID=%%a"

:: 使用PowerShell更新JSON文件（添加NoProfile参数）
powershell -NoProfile -ExecutionPolicy Bypass -Command "$json = Get-Content '%STORAGE_FILE%' -Raw | ConvertFrom-Json; $json.'telemetry.machineId' = '%NEW_MACHINE_ID%'; $json.'telemetry.macMachineId' = '%NEW_MAC_ID%'; $json.'telemetry.sqmId' = '%NEW_SQM_ID%'; $json.'telemetry.devDeviceId' = '%NEW_DEVICE_ID%'; $json | ConvertTo-Json -Depth 100 | Set-Content '%STORAGE_FILE%'"

if %errorlevel% neq 0 (
    echo 错误: storage.json更新失败
    goto :error
)
echo storage.json更新成功

echo 所有操作已完成
goto :end

:error
echo 脚本执行过程中发生错误
pause
exit /b 1

:end
echo 按任意键退出...
pause >nul
exit /b 0
